package org.frv.wispr.business;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Copyright (c) 2006 TomTom International B.V. All rights reserved.
 * TomTom PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
@Repository
@Transactional
public class UserRepositoryHibernateImpl implements UserRepository {
    SessionFactory sessionFactory;

    @Required
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public User findById(Long id) {
        return (User) sessionFactory.getCurrentSession().get(User.class,  id);

    }

    public User findByUsername(String username) {
        return (User) sessionFactory.getCurrentSession()
                .createQuery("from User where username=?")
                .setString(0, username)
                .uniqueResult();

    }

    public void save(User user) {
        sessionFactory.getCurrentSession().save(user);
    }
}
